#vertex shader

#version 130

struct Material {
	vec4 ambient;
	vec4 diffuse;
};

struct Light {
	vec4 ambient;
	vec4 diffuse;
	vec4 position;
};

uniform int maxLights;
uniform Material material;
uniform Light lights[8];

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;

in vec3 vertex_in;
in vec3 normal_in;
in vec3 texcoord_in;

out vec3 texcoord;
out vec4 color;

void main() {
	vec3 vertex = vec3(viewMatrix * modelMatrix * vec4(vertex_in, 1.0));
	vec3 normal = normalize(normalMatrix * normal_in);
	
	color = vec4(0.0, 0.0, 0.0, 0.0);
	
	for(int i = 0; i < maxLights; i++) {
		//ambient coloring
		vec4 ambient = lights[i].ambient * material.ambient;
		
		vec3 light;
	   	if(lights[i].position.w == 0.0) {
	   		light = normalize(mat3(viewMatrix) * lights[i].position.xyz);
	   	} else {
			light = normalize(lights[i].position.xyz - vertex);
	   	}
		
		//diffuse coloring
		vec4 diffuse = lights[i].diffuse * material.diffuse;
		diffuse = diffuse * max(dot(normal,light), 0.0);
		
		color += ambient + diffuse;
	}
	
	color.w = 1.0;
	
	texcoord = texcoord_in; 
	gl_Position = projectionMatrix * vec4(vertex, 1.0);
}

#fragment shader

#version 130

uniform sampler2DArray texArray;

in vec3 texcoord;
in vec4 color;

out vec4 fragColor;
 
void main() {
	vec3 texCoord = texcoord;
	
	float texFract = fract(texCoord.z);
	texCoord.z = floor(texCoord.z);
	
	vec4 pixelColor1 = texture(texArray, texCoord);
	texCoord.z += 1.0;
	vec4 pixelColor2 = texture(texArray, texCoord);
	 
    fragColor = (pixelColor1 * (1.0 - texFract) + pixelColor2 * texFract) * color;
}